/**
 * <p>Title: IpAddrUtil</p>
 * <p>Description: The tools of IpAddrUtil</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Wan Shuai Shuai
 * @version 1.0
 * <pre>Histroy:
 *       2017-2-13  Wan Shuai Shuai  Create
 *</pre>
*/
package cn.jhg.account.util;

import javax.servlet.http.HttpServletRequest;

public class IpAddrUtil {
	/**
	 * 获取终端ip
	 * @param request 请求
	 * @return String ip地址
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip =  request.getHeader("X-Real-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.indexOf(",") != -1) {
			ip = ip.substring(0, ip.indexOf(","));
		}
		return ip.trim();
	}
}
